package com.filldream.sun.bean.result;


import java.util.Collections;
import java.util.List;


/**
 * 分页返回
 * @author fishlord and RickSun
 * 2019-03-25 15:56
 */

public class PageResultEntity<T> extends BaseResultEntity{
	
	/** 总记录  */
	private long total;
	/** 记录列表  */
	private List<T> rows;
	/** 总页数  */
	private long pages;

	/**
	 * 第几页
	 */
	private long pageNo;

	private long pageSize;


	/**
	 * 空参构造方法
	 */
	public PageResultEntity(){}

	/**
	 * 整型构造方法
	 * @param code
	 * @param total
	 * @param rows
	 * @param msg
	 * @param pageNo
	 * @param pageSize
	 */
	public PageResultEntity(int code,Long total,List<T> rows,String msg,String enmsg,Integer pageNo,Integer pageSize){
		this.code = code;
		this.total = total;
		this.pageNo = pageNo.longValue();
		this.pageSize = pageSize.longValue();
		this.rows = rows;
		this.msg = msg;
		this.enmsg = enmsg;
		this.pages = (long) Math.ceil((double) this.total / this.pageSize);;
	}

	/**
	 * 默认整型成功
	 * @param total	总条数
	 * @param rows	list内容
	 * @param pageNo	当前页
	 * @param pageSize	每页的数量
	 * @return
	 */
	public static <T>PageResultEntity<T> success(long total,List<T> rows,Integer pageNo,Integer pageSize){
		ErrorCode success = ErrorCode.SUCCESS;
		return new PageResultEntity(success.getCode(),total,rows,success.getMsg(),success.getEnmsg(),pageNo,pageSize);
	}

	public static <T>PageResultEntity<T> success(long total,List<T> rows,BasePage basePage){
		ErrorCode success = ErrorCode.SUCCESS;
		return new PageResultEntity(success.getCode(),total,rows,success.getMsg(),success.getEnmsg(),basePage.getPageNo(),basePage.getPageSize());
	}

	public static <T>PageResultEntity<T> autoSuccess(long total,List<T> rows,BasePage basePage){
		ErrorCode success = ErrorCode.SUCCESS;
		if(rows.isEmpty()){
			return empty(basePage);
		}
		return new PageResultEntity(success.getCode(),total,rows,success.getMsg(),success.getEnmsg(),basePage.getPageNo(),basePage.getPageSize());
	}



	/**
	 * 自定义错误
	 * @param errorCode
	 * @return
	 */
	public static <T>PageResultEntity<T> diy(ErrorCode errorCode,BasePage basePage){
		return new PageResultEntity(errorCode.getCode(),
				0L,Collections.emptyList(),errorCode.getMsg(),errorCode.getEnmsg(),
				basePage.getPageNo(),basePage.getPageSize());
	}

	public static <T>PageResultEntity<T> diy(ErrorCode errorCode,String msg,BasePage basePage){
		return new PageResultEntity(errorCode.getCode(),
				0L,Collections.emptyList(),msg,errorCode.getEnmsg(),
				basePage.getPageNo(),basePage.getPageSize());
	}

	/**
	 * 默认空分页
	 * @return
	 */
	public static PageResultEntity empty(BasePage basePage){
		ErrorCode success = ErrorCode.SUCCESS;
		return new PageResultEntity(success.getCode(),0L,Collections.emptyList(),"empty","empty",basePage.getPageNo(),basePage.getPageSize());
	}



	/**
	 * 默认失败
	 * @return
	 */
	public static PageResultEntity fail(BasePage basePage){
		ErrorCode fail = ErrorCode.METHOD_ERR;
		return new PageResultEntity(fail.getCode(),
				0L,Collections.emptyList(),fail.getMsg(),fail.getEnmsg(),
				basePage.getPageNo(),basePage.getPageSize());
	}

	/**
	 * 默认失败
	 * @return
	 */
	public static PageResultEntity fail(ErrorCode errorCode ,BasePage basePage){
		return new PageResultEntity(errorCode.getCode(),
				0L,Collections.emptyList(),errorCode.getMsg(),errorCode.getEnmsg(),
				basePage.getPageNo(),basePage.getPageSize());
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public List<T> getRows() {
		return rows;
	}

	public void setRows(List<T> rows) {
		this.rows = rows;
	}

	public long getPages() {
		return pages;
	}

	public void setPages(long pages) {
		this.pages = pages;
	}

	public long getPageNo() {
		return pageNo;
	}

	public void setPageNo(long pageNo) {
		this.pageNo = pageNo;
	}

	public long getPageSize() {
		return pageSize;
	}

	public void setPageSize(long pageSize) {
		this.pageSize = pageSize;
	}
}
